import { request } from "../../request/index"
import { showModal,chooseAddress,showToast } from "../../utils/tools"
import regeneratorRuntime, { async, awrap } from '../../lib/runtime/runtime';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs:[
      {
        id:0,
        name:"体验问题",
        isActive:true
      },
      {
        id:1,
        name:"商品.商家投诉",
        isActive:false
      } 
    ],
    // 图片数组
    choosImg:[],
    textValue:""
  },
 

  handitemIndex(e){ 
    let {index} = e.detail
    let {tabs} = this.data 
    tabs.forEach( (v,i) => i == index ? v.isActive = true : v.isActive = false )
    this.setData({
      tabs
    })
  }, 
  // 图片上传
  handleChooseImg(){
    wx.chooseImage({ 
      success: res =>{   
        this.setData({
          choosImg : [...this.data.choosImg,...res.tempFilePaths,]
        })
      }
    })
  },
  // 图片删除 
  handleRemoveImg(e){ 
    let {index} = e.currentTarget.dataset
    let {choosImg} = this.data
    choosImg.splice(index,1)
    this.setData({
      choosImg
    })
  },
  // 文本域
  handleTextInput(e){ 
    let {value} = e.detail 
    this.setData({
      textValue:value
    })
  },

  // 提交
  async handleFormSubmit(e){
    let {textValue,choosImg} = this.data
    if(!textValue.trim()){
      wx.showToast({
        title: '输入不合法',
      })
      return
    }else{ 
      wx.showLoading({
        title: '正在上传',
      })
      choosImg.forEach( (v,i)=>{
        wx.uploadFile({ 
          filePath: v,
          // 名称
          name: 'file',
           // 土豆图床的位置
          url: 'https://imgurl.org',
          success:async res => {
            if(res){ 
              wx.hideLoading() 
              this.setData({
                choosImg:[],
                textValue:""
              })  
              wx.switchTab({
                url: '/pages/user/index',
              })
            }
          }
        })
      })
    }
  }
})